package com.icas.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.icas.entity.resultentity.AttendCountEntity;
import com.icas.enums.EndStatusEnum;
import com.icas.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: tocchen
 * @Date: 2022/4/19 12:37
 * @Description
 **/
@Component
public class CountService {
    @Autowired
    private MongoTemplate mongoTemplate;

    public static CountService db;

    public static final CountService defaultUserServer = new CountService();

    @PostConstruct
    public void init() {
        db = this;
        db.mongoTemplate = this.mongoTemplate;
    }

    /**
     * TODO End情况
     * 获取学生的历史签到统计
     * @param courseId 课程id
     * @param stuId 学生id
     */
    public AttendCountEntity getAttendCount(String courseId, String stuId){
        List<AggregationOperation> parm = new ArrayList<>(7);
        parm.add(Aggregation.match(Criteria.where("courseId").is(courseId)));
        parm.add(Aggregation.project("stuAttend", "course"));
        parm.add(Aggregation.unwind("stuAttend"));
        parm.add(Aggregation.match(Criteria.where("stuAttend.stuId").is(stuId)));
        parm.add(Aggregation.group("stuAttend.status").count().as("count"));
        parm.add(Aggregation.project("stuAttend.status", "count").and("status").previousOperation());

        Aggregation aggregation = Aggregation.newAggregation(parm);
        List<Object> res = db.mongoTemplate.aggregate(aggregation, Constant.ATTEND_SHEET_TABLE_NAME, Object.class).getMappedResults();
        AttendCountEntity attendCountEntity = AttendCountEntity.initInstance();

        res.forEach(v -> {
            JSONObject stuAttend = JSON.parseObject(JSON.toJSONString(v), new TypeReference<JSONObject>() {
            });
            String status = (String) stuAttend.get("status");
            if (Objects.equals(status, EndStatusEnum.attendances.getStatus()+"")) {
                attendCountEntity.setAttendances(Integer.parseInt(String.valueOf(stuAttend.get("count"))));
            } else if (Objects.equals(status, EndStatusEnum.leave.getStatus()+"")) {
                attendCountEntity.setLeave(Integer.parseInt(String.valueOf(stuAttend.get("count"))));
            } else if (Objects.equals(status, EndStatusEnum.full_time.getStatus()+"")) {
                attendCountEntity.setFull_time(Integer.parseInt(String.valueOf(stuAttend.get("count"))));
            }else if (Objects.equals(status, EndStatusEnum.ask.getStatus()+"")){
                attendCountEntity.setAsk(Integer.parseInt(String.valueOf(stuAttend.get("count"))));
            }else if (Objects.equals(status, EndStatusEnum.late_leave.getStatus()+"")){
                attendCountEntity.setLate_leave(Integer.parseInt(String.valueOf(stuAttend.get("count"))));
            }else {
                attendCountEntity.setLate(Integer.parseInt(String.valueOf(stuAttend.get("count"))));
            }
        });
        return attendCountEntity;
    }

    /**
     * 获取所有学生的在课程的签到统计
     * @param courseId 课程Id
     * @return AttendCountEntity实体类
     */
    public  AttendCountEntity getAllStuHistoryAttend(String courseId) {
        List<AggregationOperation> operations = new ArrayList<>();
        operations.add(Aggregation.match(Criteria.where("courseId").is(courseId)));
        operations.add(Aggregation.match(Criteria.where("status").is("end")));
        operations.add(Aggregation.project("stuAttend", "course"));
        operations.add(Aggregation.unwind("stuAttend"));
        operations.add(Aggregation.group("stuAttend.status").count().as("count"));
        operations.add(Aggregation.project("stuAttend.status", "count").and("status").previousOperation());
        Aggregation aggregation = Aggregation.newAggregation(operations);
        List<Object> res = db.mongoTemplate.aggregate(aggregation, Constant.ATTEND_SHEET_TABLE_NAME, Object.class).getMappedResults();
        AttendCountEntity attendCountEntity = AttendCountEntity.initInstance();

        res.forEach(v -> {
            JSONObject stuAttend = JSON.parseObject(JSON.toJSONString(v), new TypeReference<JSONObject>() {
            });
            String status = (String) stuAttend.get("status");
            if (Objects.equals(status, EndStatusEnum.attendances.getStatus()+"")) {
                attendCountEntity.setAttendances(Integer.parseInt(String.valueOf(stuAttend.get("count"))));
            } else if (Objects.equals(status, EndStatusEnum.leave.getStatus()+"")) {
                attendCountEntity.setLeave(Integer.parseInt(String.valueOf(stuAttend.get("count"))));
            } else if (Objects.equals(status, EndStatusEnum.full_time.getStatus()+"")) {
                attendCountEntity.setFull_time(Integer.parseInt(String.valueOf(stuAttend.get("count"))));
            }else if (Objects.equals(status, EndStatusEnum.ask.getStatus()+"")){
                attendCountEntity.setAsk(Integer.parseInt(String.valueOf(stuAttend.get("count"))));
            }else if (Objects.equals(status, EndStatusEnum.late_leave.getStatus()+"")){
                attendCountEntity.setLate_leave(Integer.parseInt(String.valueOf(stuAttend.get("count"))));
            }else {
                attendCountEntity.setLate(Integer.parseInt(String.valueOf(stuAttend.get("count"))));
            }
        });
        return attendCountEntity;
    }

}
